import 'package:flutter_module/page/welfare_ranking/welfare_ranking_dialog.dart';
import 'package:flutter_module/page/welfare_ranking/welfare_ranking_home_state.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import '../../common/base/logic/BasicLogic.dart';
import '../../common/channel/ChannelConstant.dart';
import '../../common/http/ApiConstant.dart';
import '../../common/http/YZHttpUtils.dart';
import '../../common/router/RouterPath.dart';
import '../../common/third/analytics/YZAnalytics.dart';
import '../../model/article_url_config.dart';
import '../../model/welfare_team_list_info.dart';

class WelfareRankingHomeLogic extends BasicLogic {
  final state = WelfareRankingHomeState();

  @override
  void onInit() {
    super.onInit();
    requestRuleDialogContent();
  }

  void requestRuleDialogContent() {
    HttpUtils.get(api_welfare_article_url_config, params: {'id': 8},
        successCallBack: (data, total) {
          dealRuleDialogContent(data, total);
        },
        errorCallBack: (e) {
          SmartDialog.dismiss();
        });
  }

  void dealRuleDialogContent(data, int total) {
    ArticleUrlConfigEntity articleUrlConfigEntity = ArticleUrlConfigEntity
        .fromJson(data);
    state.htmlContent = articleUrlConfigEntity.context ?? "";
  }

  void selectBtnClicked(WelfareTeamListInfo? user) {
    BoostNavigator.instance.push(ROUTER_WELFARE_USERINFO,
        arguments: {
          KEY_ROUTER_PARAMETERS:
          '?$PARAM_ORG_ID=${user?.orgId ??"0"}&$PARAM_USER_ID=${user?.orgUserId ??"0"}'
        },
        withContainer: true);
  }
  void jumpWelfareDo() {
    BoostNavigator.instance.push(ROUTER_WELFARE_DO,
        arguments: {
          KEY_ROUTER_PARAMETERS: ''
        },
        withContainer: true);
  }


  void shareWelfarePoster(WelfareTeamListInfo? user) {
    //直接用BoostNavigator的跳转原生，会调用原生的回调方法跳转
    BoostNavigator.instance.push(ROUTER_WELFARE_RANKING_POSTER,
        arguments: {
          KEY_ROUTER_PARAMETERS:
          '?$PARAM_WELFARE_TIME_TYPE=${state.timeType
              .value}&$PARAM_WELFARE_COUNTING_TYPE=${state.countingType
              .value}&$PARAM_WELFARE_NO=${user?.rank ?? 0}&$PARAM_WELFARE_TIME=${user?.time ??
              '0'}&$PARAM_WELFARE_HOT=${user?.hot ?? '0'}'
        },
        withContainer: true);
    if(state.countingType.value==1){
      YZAnalytics.track("S00082", "公益圈-公益时长榜-分享海报", "上进社区", null);
    }else{
      YZAnalytics.track("S00087", "公益圈-人气榜-分享海报", "上进社区", null);
    }

  }

  void showWelfareRule() {
    SmartDialog.show(
        backType: SmartBackType.normal,
        clickMaskDismiss: false,
        builder: (_) {
          return WelfareRankingDialog(htmlContent: state.htmlContent);
        },
    );
  }
}
